loops and iteration
Two iteration or loop structures are supported:
DO ... LOOP - DO loops
FOR ... NEXT - FOR loops
DO ... LOOP
DO loops are the most flexible form of iteration. WHILE and UNTIL can be appended to
DO, LOOP, both, or neither.
DO options
The order of execution in DO loops is altered when any of the following statements
execute:
DO DO continue at the top (the DO)
DO LOOP continue at the bottom (the LOOP)
EXIT DO exit (past the LOOP)
The following example illustrates these options:
DO WHILE (x < y) ' jump past
LOOP if (x >= y)
Shuffle (@k, @n, @z) ' new values for k, n, z
PRINT "Start DO" '
IF z THEN DO LOOP ' jump down to LOOP
PRINT "z = 0" '
IF k THEN DO DO ' jump up to DO
PRINT "k = 0" '
IF n THEN EXIT DO ' jump past LOOP
PRINT "n = 0" '
LOOP UNTIL (j > p) ' jump to DO if (j <= p)
EXIT DO example
The following example utilizes two DO loops. The outer DO loop has no test
conditions on the DO or LOOP statements. This would cause an endless loop condition
if not for the EXIT DO that executes when testString$ is empty.
FUNCTION PlayTheHashGame ()
$BYTE0 = BITFIELD (8, 0)
'
DO
testString$ = INLINE$ ("Compute hash for string ===>>
")
IFZ testString$ THEN EXIT DO
stringLength = LEN (testString$)
byteOffset = 0
hash = 0
DO WHILE (byteOffset < stringLength)
testByte = testString${byteOffset}
hash = hash + testByte
INC byteOffset
' DANGER: don't forget
this line !!!
LOOP
hash = hash{$BYTE0}
PRINT "The hash for "; testString$; " is: ";
HEXX$(hash, 2)
LOOP
PRINT "***** DONE *****"
END FUNCTION